//
//  NSDictionary+Safe.m
//  niuguwang
//
//  Created by BrightLi on 2021/5/15.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

#import "NSDictionary+Safe.h"
#import "YMSafeUtil.h"

@implementation NSDictionary(Safe)

+ (void) safe_fixed
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [YMSafeUtil swizzleClass:self
                       sel1:@selector(dictionaryWithObjects:forKeys:count:)
                       sel2:@selector(safe_dictionaryWithObjects:forKeys:count:)];
    });
}

+ (instancetype) safe_dictionaryWithObjects:(id _Nonnull const [])objects forKeys:(id<NSCopying> _Nonnull const [])keys
                                      count:(NSUInteger)cnt
{
    id instance = nil;
    @try {
        instance = [self safe_dictionaryWithObjects:objects forKeys:keys count:cnt];
    }
    @catch (NSException *exception) {
        [YMSafeUtil stackLog:exception start:3];
        //处理错误的数据，然后重新初始化一个字典
        NSUInteger index = 0;
        id _Nonnull __unsafe_unretained newObjects[cnt];
        id _Nonnull __unsafe_unretained newkeys[cnt];
        for (int i = 0; i < cnt; i++) {
            if (objects[i] && keys[i]) {
                newObjects[index] = objects[i];
                newkeys[index] = keys[i];
                index++;
            }
        }
        instance = [self safe_dictionaryWithObjects:newObjects forKeys:newkeys count:index];
    }
    @finally {
        return instance;
    }
}

@end

@implementation NSMutableDictionary(Safe)

+ (void) safe_fixed
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls=NSClassFromString(@"__NSDictionaryM");
        [YMSafeUtil swizzleInstance:cls
                          sel1:@selector(setObject:forKey:)
                          sel2:@selector(safe_setObject:forKey:)];
        if(SSafeIsiOS(11)){
            [YMSafeUtil swizzleInstance:cls
                              sel1:@selector(setObject:forKeyedSubscript:)
                              sel2:@selector(safe_setObject:forKeyedSubscript:)];
        }
        [YMSafeUtil swizzleInstance:cls
                          sel1:@selector(removeObjectForKey:)
                          sel2:@selector(safe_removeObjectForKey:)];
    });
}

- (void)safe_setObject:(id)obj forKey:(id<NSCopying>)key
{
    @try {
        if(!obj){
            [YMSafeUtil log:@"setObject不能是空值"];
            return;
        }
        [self safe_setObject:obj forKey:key];
    }
    @catch (NSException *exception) {
        [YMSafeUtil stackLog:exception start:3];
    }
}

- (void) safe_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
{
    @try {
        [self safe_setObject:obj forKeyedSubscript:key];
    }
    @catch (NSException *exception) {
        [YMSafeUtil stackLog:exception start:3];
    }
    @finally {
    }
}

- (void) safe_removeObjectForKey:(id)aKey
{
    @try {
        [self safe_removeObjectForKey:aKey];
    }
    @catch (NSException *exception) {
        [YMSafeUtil stackLog:exception start:3];
    }
    @finally {
    }
}

@end
